package com.bigdatastudio.notice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bigdatastudio.common.Utils.R;
import com.bigdatastudio.common.entity.Notice;
import com.bigdatastudio.common.entity.NoticeType;
import com.bigdatastudio.common.entity.Type;
import com.bigdatastudio.notice.server.INoticeService;
import com.bigdatastudio.notice.server.INoticeTypeService;
import com.bigdatastudio.notice.server.ITypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/noticeType")
public class NoticeTypeController {
    @Autowired
    private INoticeService noticeService;

    @Autowired
    private INoticeTypeService noticeTypeService;

    @Autowired
    private ITypeService iTypeService;

    @PostMapping("update")
    public R addorupdate(@RequestBody NoticeType noticeType) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getId, noticeType.getNoticeId());
        Notice one = noticeService.getOne(queryWrapper);
        if (one.getIsDelete() == 1) {
            return R.error("失败，没有该数据");
        } else {
            UpdateWrapper<NoticeType> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("notice_id", noticeType.getNoticeId());
            updateWrapper.set("type_id", noticeType.getTypeId());
            noticeTypeService.update(updateWrapper);
            return R.ok("成功");
        }
    }

    @GetMapping("getAll")
    public R getAll() {
        List<Type> list = iTypeService.list(null);
        System.out.println("list = " + list);
        return R.ok(list, "成功");
    }
}
